<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从结果集中取得一行作为关联数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-escape-string.html">« mysql_escape_string</a></li>
      <li style="float: right;"><a href="function.mysql-fetch-assoc.html">mysql_fetch_assoc »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>从结果集中取得一行作为关联数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-fetch-array" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_fetch_array</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_fetch_array</span> &mdash; <span class="dc-title"> 从结果集中取得一行作为关联数组</span></p>

   </div>
  <div class="refsect1 description" id="refsect1-function.mysql-fetch-array-description">
    <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>mysql_fetch_array</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = ?</span></span>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_fetch_array()</strong></span> 是
     <span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span>
     的扩展版本。除了将数据以数字索引方式储存在数组中之外，还可以将数据作为关联索引储存，用字段名作为键名。
    </p>
    <p class="para">
     有一点很重要必须指出，用
     <span class="function"><strong>mysql_fetch_array()</strong></span> 并<em class="emphasis">不明显</em> 比用
     <span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span> 慢，而且还提供了明显更多的值。
    </p>
  </div>

 <div class="refsect1 parameters" id="refsect1-function.mysql-fetch-array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">result</code></dt>
<dd>
<p class="para"><span class="type">resource</span>
型的结果集。此结果集来自对 <span class="function"><strong>msql_query()</strong></span>
的调用。</p></dd>

    
     <dt>
<code class="parameter">result_type</code></dt>

     <dd>

      <p class="para">
        接受以下常量值：
        <strong><code>MSQL_ASSOC</code></strong>，<strong><code>MSQL_NUM</code></strong> 和
        <strong><code>MSQL_BOTH</code></strong>，默认为 <strong><code>MSQL_BOTH</code></strong>。如果用了
        MYSQL_BOTH，将得到一个同时包含关联和数字索引的数组。用
        MYSQL_ASSOC 只得到关联索引（如同 <span class="function"><a href="function.mysql-fetch-assoc.html" class="function">mysql_fetch_assoc()</a></span> 那样），用
        MYSQL_NUM 只得到数字索引（如同 <span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span> 那样）。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.mysql-fetch-array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回根据从结果集取得的行生成的数组，如果没有更多行则返回 <strong><code>false</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.mysql-fetch-array-examples">
  <h3 class="title">范例</h3>
  <p class="para">    
     <div class="example" id="example-1752">
      <p><strong>示例 #1 <span class="function"><strong>msql_fetch_array()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$con&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">msql_connect</span><span style="color: #007700">();<br />if&nbsp;(!</span><span style="color: #0000BB">$con</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Server&nbsp;connection&nbsp;problem:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">msql_error</span><span style="color: #007700">());<br />}<br /><br />if&nbsp;(!</span><span style="color: #0000BB">msql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$con</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Database&nbsp;connection&nbsp;problem:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">msql_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">msql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;id,&nbsp;name&nbsp;FROM&nbsp;people'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$con</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Query&nbsp;execution&nbsp;problem:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">msql_error</span><span style="color: #007700">());<br />}<br /><br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">msql_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MSQL_ASSOC</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">':&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">msql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.mysql-fetch-array-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><strong>msql_fetch_row()</strong></span></li>
    <li class="member"><span class="function"><strong>msql_fetch_object()</strong></span></li>
    <li class="member"><span class="function"><strong>msql_data_seek()</strong></span></li>
    <li class="member"><span class="function"><strong>msql_result()</strong></span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>